package com.liu.head.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.liu.common.controller.BaseController;
import com.liu.common.pojo.InfoEnum;
import com.liu.common.util.ConstatFinalUtil;
import com.liu.common.util.PageInfoUtil;
import com.liu.users.pojo.ARegion;
import com.liu.users.pojo.AUsers;
import com.liu.users.service.IUsersService;



@Controller
@RequestMapping("/head/users/")
public class UsersHeadController extends BaseController
{
	@Autowired
	private IUsersService usersService ;
	/**
	 * 用户列表页面
	 * @return
	 */
	@RequestMapping("/usersList")
	public String usersList(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.LOGGER.info("==打开列表页面==");
		/* 分页 */
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(request);
		/*搜索*/
		Map<String, Object> condMap = proccedSearch(request);
		/*====邮箱状态开始=====*/
		 String emailStatus = request.getParameter("emailStatus");
		 if(emailStatus == null)
		 {
			 emailStatus = "" ;
		 }
		 condMap.put("emailStatus", emailStatus);
		 request.setAttribute("emailStatus", emailStatus);
		/*====邮箱状态结束=====*/
		/*调用service方法*/
		List<AUsers> list = this.usersService.findCondListUsersService(pageInfoUtil,condMap);
		/* 存储四大作用域====>request */
		model.addAttribute("list", list);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		return null ;
	}
	/**
	 * 退出操作
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session,Model model )
	{
		ConstatFinalUtil.LOGGER.info("==退出操作==");
		session.removeAttribute("usersSess");
		session.removeAttribute("lastLoginTime");
		return "redirect:/login";
	}
	/**
	 * 打开更新页面
	 * @param operType
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(String operType,int id,Model model)
	{
		ConstatFinalUtil.LOGGER.info("====打开更新页面====");
		/*传入一个id*/
		Map<String,Object>condMap = new HashMap<String,Object>();
		condMap.put("id", id);
		AUsers users = this.usersService.findOneUsersService(condMap);
		model.addAttribute("users", users);
		model.addAttribute("operType", operType);
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/*查询状态为启用的地区*/
			condMap.clear();
			condMap.put("status", "1");
			List<ARegion> regionList = this.usersService.findCondListRegionService(null, condMap);
			model.addAttribute("regionList", regionList);
			return "/head/users/updateInfo" ;
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/*修改个人信息*/
			return "/head/users/updatePass" ;
		}else if("updateEmail".equalsIgnoreCase(operType))
		{
			/*重置邮箱*/
			return "/head/users/updateEmail" ;
		}else if("updatePhoto".equalsIgnoreCase(operType))
		{
			/*修改头像*/
			return "/head/users/updatePhoto" ;
		}
		/*详情信息*/
		return "/head/users/usersInfo" ;
	}
	/**
	 * 更新提交操作
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(String operType,int id,
			MultipartFile file,
			HttpServletRequest request ,Model model) throws IOException
	{
		ConstatFinalUtil.LOGGER.info("====usersUpdateSubmit====");
		this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_OPER_ERROR.getCode() + "") ; 
		
		/* 传入一个id */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("id", id);
		AUsers users = this.usersService.findOneUsersService(condMap);
		model.addAttribute("users", users);
		model.addAttribute("operType", operType);
		
		Map<String,Object> paramsMap = new HashMap<String, Object>();
		boolean flag = false ; 
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 修改个人信息 */
			String nickName = request.getParameter("nickName");
			String qq = request.getParameter("qq");
			String phone = request.getParameter("phone");
			String regionId = request.getParameter("regionId");
			
			users.setNickName(nickName);
			users.setQq(qq);
			users.setPhone(phone);
			users.setRegionId(Integer.valueOf(regionId));
			
			flag = true ;
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/* 修改个人信息 */
		}else if("updateEmail".equalsIgnoreCase(operType))
		{
			/* 重置邮箱 */
		}else if("updatePhoto".equalsIgnoreCase(operType))
		{
			/* 修改头像 */
			String fileName = file.getOriginalFilename();
			InputStream is = file.getInputStream();
			paramsMap.put("fileName",fileName);
			paramsMap.put("is", is);
			
			flag  = true ;
		}
		
		if(flag)
		{
			/* 更新用户 */
			JSONObject resultJson = this.usersService.updateOneUsersService(users,paramsMap);
			ConstatFinalUtil.LOGGER.info("数据库返回:{}",resultJson.toJSONString());
			/* 提示信息 */
			this.info = resultJson.getString(InfoEnum.VAR_INFO.getInfo());
		}
		/*保存信息*/
		model.addAttribute("info", info);
		return this.usersUpdate(operType,id,model) ; 
	}
}
